<?php

namespace app\common\model\mall\oper;

use think\Model;

class Help extends Model
{
    // 表名
    protected $name = 'mall_oper_help';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'status_text'
    ];

    public function getStatusList()
    {
        return ['normal' => __('Normal'),'hidden' => __('Hidden')];
    }

    public function getCoverimgAttr($value, $data)
    {
        $mallcdn = config("mall.mallcdn");
        if($mallcdn && !stristr($value,$mallcdn))
            return $mallcdn ? $mallcdn.$value : cdnurl($value);
        else
            return cdnurl($value);
    }

    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 获取帮助左侧列表
     */
    public function helpList(){
        $arts = cache('arts');
        if(!$arts){
        	$rs = collection($this->alias('a')
                ->join('__MALL_OPER_HELPCAT__ ac','a.cat_id=ac.id','inner')
                ->field('a.id,a.cat_id,a.articleTitle,ac.name')
                ->where(['a.status'=>'normal','a.showis'=>1,'ac.state'=>1,'ac.showis'=>1,'ac.pid'=>1])
                ->order('a.weigh asc')->cache(true)->select())->toArray();
            //同一分类下的文章放一起
            $catName = [];
            $arts = [];
            foreach($rs as $k=>$v){
                if(in_array($v['name'],$catName)){
                    $arts[$v['name'].'-'.$v['cat_id']][] = $v;
                }else{
                    $catName[] = $v['name'];
                    $arts[$v['name'].'-'.$v['cat_id']][] = $v;
                }
            }
            cache('arts',$arts,86400);
        }
        return $arts;
    }
    /**
     *  根据id获取帮助文章及其分类
     */
    public function getHelpById(){
        $id = (int)input('id');
        // 统计文章访问量
        $this->where(['id'=>$id])->setInc('visitorNum',1);
        $rs = $this->alias('a')
            ->join('__MALL_OPER_HELPCAT__ ac','a.cat_id=ac.id','inner')
            ->where('ac.pid=1 and a.status="normal" and a.showis=1 and a.id='.$id)
            ->field("a.id,a.cat_id,a.articleTitle,a.coverimg,a.articleKey,a.visitorNum,a.updatetime,a.acontent")
            ->cache(true)
            ->find();
        return $rs;
    }
    /**
     *  根据id获取资讯文章及其分类
     */
    public function getNewsById($id = 0){
        $id = $id>0?$id:(int)input('id');
        // 统计文章访问量
        $this->where(['id'=>$id])->setInc('visitorNum',1);
        $rs = db("mall_oper_help")->alias('a')
            ->field('a.*,ac.name,u.nickname')
            ->join('__MALL_OPER_HELPCAT__ ac','a.cat_id=ac.id','inner')
            ->join("__USER__ u","a.user_id=u.id","left")
            ->where('a.cat_id<>1 and ac.pid<>1 and a.status="normal" and a.showis=1 ')
            ->order('a.weigh asc')->cache(true)->find($id);
        return $rs;
    }

    /**
     * 获取资讯列表【左侧分类】
     */
    public function NewsList(){
        //读取分类树
        $list =  $this->getTree();
        foreach($list as $k=>$v){
            if(!empty($v['children'])){
                foreach($v['children'] as $k1=>$v1){
                    // 二级分类下的文章总条数
                    $list[$k]['children'][$k1]['newsCount'] = $this
                        ->where(['cat_id'=>$v1['id'], 'status'=>'normal','showis'=>1])
                        ->cache(true)->count();
                }
            }
        }
        return $list;
    }

    public function getTree(){
        $artTree = cache('artTree');
        if(!$artTree){
            $data = db('mall_oper_helpcat')->field('name,id,pid')
                ->where('pid <> 1 and id <> 1 and state=1 and showis=1')
                ->cache(true)->select();
            $artTree = $this->_getTree($data, 0);
            cache('artTree',$artTree,86400);
        }
        return $artTree;
    }
    public function _getTree($data,$parentId){
        $tree = [];
        foreach($data as $k=>$v){
            if($v['pid']==$parentId){
                // 再找其下级分类
                $v['children'] = $this->_getTree($data,$v['id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }
    /**
     *	根据分类id获取文章列表
     */
    public function nList(){
        $catId = (int)input('catId');
        $where =  ['a.status'=>"normal",'a.showis'=>1,'ac.state'=>1,'ac.showis'=>1];
        if($catId){
            $where += ['a.cat_id'=>$catId];
        }
        $rs = $this->alias('a')
            ->join('__MALL_OPER_HELPCAT__ ac','a.cat_id=ac.id','inner')
            ->join("__USER__ u","a.user_id=u.id","left")
            ->field('a.*,u.nickname')
            ->where($where)
            ->order('a.weigh asc,a.createtime desc')
            ->where('ac.pid','<>',1)
            ->cache(true)
            //->fetchSql()
            ->select();
        return $rs;
    }

    /**
     * 面包屑导航
     */
    public function bcNav(){
        $catId = (int)input('catId'); //分类id
        $artId = (int)input('id'); 	//文章id
        $data = db('mall_oper_helpcat')->field('id,pid,name')->cache(true)->select();
        if($artId){
            $catId = $this->where('id',$artId)->value('cat_id');
        }
        $bcNav = $this->getParents($data,$catId,$isClear=true);
        return $bcNav;

    }
    /**
     * 获取父级分类
     */
    public function getParents($data, $catId,$isClear=false){
        static $bcNav = [];
        if($isClear)
            $bcNav = [];
        foreach($data as $k=>$v){
            if($catId == $v['id']){
                if($catId!=0){
                    $this->getParents($data, $v['pid']);
                    $bcNav[] = $v;
                }
            }
        }
        return $bcNav;
    }

    /**
     *  记录解决情况
     */
    public function recordSolve(){
        $articleId =  (int)input('aid');
        $status =  (int)input('status');
        if($status==1){
            $rs = $this->where('id',$articleId)->setInc('solve');
        }else{
            $rs = $this->where('id',$articleId)->setInc('unsolve');
        }
        return $rs;
    }

    /**
     * 获取资讯中心的子集分类id
     */
    public function getChildIds(){
        $ids = [];
        $data = db('mall_oper_helpcat')->cache(true)->select();
        foreach($data as $k=>$v){
            if($v['pid']!=1 && $v['id']!=1 && $v['pid']!=0 ){
                $ids[] = $v['id'];
            }
        }
        return $ids;
    }

    /**
     * 获取咨询中中心所有文章
     */
    public function getHelp(){
        // 获取咨询中心下的所有分类id
        $ids = $this->getChildIds();
        $rs = $this->alias('a')
            ->field('a.*')
            ->join('__MALL_OPER_HELPCAT__ ac','a.cat_id=ac.id','inner')
            ->where(['a.state'=>1,'a.showis'=>1,'ac.state'=>1,'ac.showis'=>1])
            ->where(['a.cat_id'=>['in',$ids],'ac.pid'=>['<>',7]])->order('a.weigh asc')
            ->distinct(true)
            ->cache(true)
            ->paginate(15);
        return $rs;
    }

    /**
     * 获取指定分类下的文章
     */
    public function getHelpByCat($catId){
        $ids = $this->getChildIds();
        $rs = $this->alias('a')
            ->field('a.*')
            ->join('__MALL_OPER_HELPCAT__ ac','a.cat_id=ac.id','inner')
            ->where(['a.state'=>1,'a.showis'=>1,'ac.state'=>1,'ac.showis'=>1])
            ->where(['a.cat_id'=>['in',$ids],'ac.pid'=>['<>',1]])->order('a.weigh asc')
            ->distinct(true)
            ->cache(true)
            ->select();
        $data = [];
        if(!empty($rs)){
            foreach($rs as $key =>$v){
                $data[$v['id']] = $v;
            }
        }
        return $data;
    }

    /**
     * 根据指定数量获取分类及帮助
     * @param $pnum int 父级记录数
     * @param $cnum int 子记录数
     */
    public static function getCatHelps($pnum = 5,$cnum = 5){
        $cats = db('mall_oper_helpcat')
            ->where(['type'=>1, 'state'=>1, 'showis' => 1, 'pid'=>1])
            ->field("name,id")->order('weigh asc')->limit($pnum)->select();
        if(!empty($cats)){
            foreach($cats as $key =>$v){
                $cats[$key]['articlecats'] = db('mall_oper_help')
                    ->where(['state'=>1,'showis' => 1,'cat_id'=>$v['id']])
                    ->field("id, cat_id, articleTitle")
                    ->order('weigh asc,createtime asc')->limit($cnum)->select();
            }
        }
        return $cats;
    }
}
